package com.websocket;

import com.websocket.client.WebSocketClient;
import com.websocket.handshake.ServerHandshake;
import com.websocket.impl.WSListener;
import com.websocket.util.LogWSUtils;

import java.net.URI;

/**
 * websocket连接/发送/接收
 */
public class WebSocketUtils {
    private final String TAG = this.getClass().getSimpleName();
    private static WebSocketUtils utils = null;

    public WebSocketClient webSocketClient;
    public WSListener wsListener;
    private int NUM = 1;

    public String wsurl = "ws://119.23.29.8:8001/";

    public static WebSocketUtils getInstance(){
        if (utils == null){
            synchronized (WebSocketUtils.class){
                if (utils == null){
                    utils = new WebSocketUtils();
                }
            }
        }
        return utils;
    }

    public void openWS(String url, WSListener listener){
        wsurl = url;
        wsListener = listener;
        startSoclet();
    }

    /**连接服务器**/
    private void startSoclet(){
        try {
            URI uri = new URI(wsurl);
            LogWSUtils.log(TAG, "连接ws=" + wsurl);
            webSocketClient = new WebSocketClient(uri) {
                @Override
                public void onOpen(ServerHandshake handshakedata) {
                    LogWSUtils.log(TAG, "onOpen");
                    NUM = 1;
                }

                @Override
                public void onMessage(String message) {
                    LogWSUtils.log(TAG, "onMessage=" + message);
                    if (wsListener != null){
                        wsListener.onWSMessage(message);
                    }
                }

                @Override
                public void onClose(int code, String reason, boolean remote) {
                    LogWSUtils.log(TAG,
                            "onClose="
                                    + "--code：" + code
                                    + "--reason：" + reason
                                    + "--remote：" + remote);
                    if (code != 1000){
                        reConnet();
                    }
                    webSocketClient = null;
                }

                @Override
                public void onError(Exception ex) {
                    LogWSUtils.log(TAG, "onError=" + ex.getMessage());
                    reConnet();
                }
            };
            webSocketClient.connect();
        }catch (Exception e){
            LogWSUtils.log(TAG, "onError=" + e.getMessage());
            e.printStackTrace();
        }
    }

    /***关闭连接***/
    public void close(){
        if (webSocketClient != null){
            webSocketClient.close();
            webSocketClient = null;
            LogWSUtils.log(TAG, "close=关闭连接");
        }
    }

    /***断开重连 3次***/
    private void reConnet(){
        if (NUM <= 3){
            close();
            LogWSUtils.log(TAG, "reConnet=websocket第" + NUM + "重新连接");
            startSoclet();
            NUM ++;
        }
    }

    /***发送数据***/
    public void sendData(String msg){
        try {
            if (webSocketClient != null && webSocketClient.isOpen()){
                webSocketClient.send(msg);
                LogWSUtils.log(TAG, "sendData=" + msg);
            }else {
                startSoclet();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
